<?php
$host = @$_POST['host'] ;
$username = @$_POST['username'] ;
$password = @$_POST['password'] ;
$databaseName  = @$_POST['databaseName'] ;
$Year = @$_POST['year'] ;

$oldmatk = @$_POST['oldmatk'] ;
$matk = @$_POST['matk'] ;
$tentk = @$_POST['tentk'] ;

$loaitk = @$_POST['loaitk'] ;
$mode = @$_POST['mode'] ;

$codulieu = 0;
$succ = 1;

$link = mysqli_connect($host,$username,$password,$databaseName) or die("Error " . mysqli_error($link));

if (!mysqli_set_charset($link, "utf8")) {
} else {
}

$sql = "select * from DMTK" ;
$checktable = mysqli_query($link, $sql); 
if($checktable === FALSE)
{
		echo "1";
		return;
}

$succ = FALSE;
if ($mode == "add")
{
	$sql = "select * from DMTK where matk = '".$matk."' LIMIT 1" ;
	$checktable = mysqli_query($link, $sql);
	if ($checktable->num_rows ==0)
		$succ = true;

}
else
{
	//echo($matk == $oldmatk);
	if ($matk == $oldmatk) //ok
	{
		$succ = true;
	}
	else
	{
		$sql = "select * from DMTK where matk = '".$matk."' LIMIT 1" ;
		$checktable = mysqli_query($link, $sql);
		if ($checktable->num_rows ==0)
			$succ = true;

	}
}

if($succ == true)
{
}
else
{
	echo "2";
	return;
}

if ($mode == "add")
{
	$mysql_pa = "INSERT INTO DMTK SET ";
	$mysql_pa .= "  matk='". $matk. "', ";
	$mysql_pa .= "  tentk='". $tentk. "', ";
	$mysql_pa .= "  loaitk='". $loaitk. "' ";
}
else
{
	$mysql_pa = "UPDATE DMTK SET ";
	$mysql_pa .= "  matk='". $matk. "', ";
	$mysql_pa .= "  tentk='". $tentk. "', ";
	$mysql_pa .= "  loaitk='". $loaitk. "' ";	
	$mysql_pa .= "  where matk ='". $oldmatk. "' LIMIT 1 ";
}

$succ = mysqli_query($link, $mysql_pa);
if ($succ == false)
{
	echo "3".$mysql_pa;
}


$sql = "select * from filecache where filename = 'dmtk' LIMIT 1" ;
$checktable = mysqli_query($link, $sql);
if ($checktable->num_rows ==0)

	$mysql = "INSERT INTO  filecache (`filename` ,`last_modified`) VALUES ('dmtk', '". date("Y-m-d H:i:s")."');";
else
	$mysql = "UPDATE  filecache SET last_modified = '".date("Y-m-d H:i:s"). "' where filename = 'dmtk'";
$succ = mysqli_query($link, $mysql);

mysqli_close($link);

?> 

